package com.beemans.corekit.integration.toolkit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.LinkedList;

public final class ActivityUtils {

    public static boolean isActivityAlive(final Activity activity) {
        return activity != null && !activity.isFinishing() && !activity.isDestroyed();
    }

    @Nullable
    public static Activity currentActivity() {
        return UtilsActivityLifecycleImpl.Holder.INSTANCE.currentActivity();
    }

    public static LinkedList<Activity> getActivityList() {
        return UtilsActivityLifecycleImpl.Holder.INSTANCE.getActivityList();
    }

    public static void startActivity(@NonNull Intent intent) {
        startActivity(intent, null);
    }

    public static void startActivity(@NonNull Intent intent, @Nullable Bundle bundle) {
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        try {
            AppUtils.getApp().startActivity(intent);
        } catch (Exception ignored) {
        }
    }

    /**
     * Start an Activity
     */
    public static void startActivity(@NonNull Class<? extends Activity> cls) {
        startActivity(cls, null);
    }

    /**
     * Start an Activity
     */
    public static void startActivity(
            @NonNull Class<? extends Activity> cls,
            @Nullable Bundle bundle
    ) {
        if (AppUtils.getApp() != null) {
            final Intent intent = new Intent(AppUtils.getApp(), cls);
            startActivity(intent, bundle);
        }
    }

    /**
     * Finish a specific Activity
     */
    public static void finishActivity(@NonNull Activity activity) {
        finishActivity(activity, true);
    }

    /**
     * Finish a specific Activity
     */
    public static void finishActivity(@NonNull Activity activity, boolean isLoadAnim) {
        activity.finish();

        if (!isLoadAnim) {
            activity.overridePendingTransition(0, 0);
        }
    }
}